package com.demo.modules.sys.form;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.common.base.entity.PageQueryEntity;
import com.demo.modules.sys.entity.SysUserEntity;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.ObjectUtils;

/**
 * 查询参数
 */
@Getter
@Setter
public class SysUserQueryForm extends PageQueryEntity {

    /**
     * 编码或名称关键字
     */
    private String key;

    /**
     * 所属组织ID
     */
    private Integer userOrg;

    /**
     * 是否管理用户
     */
    private Integer isAdmin;

    /**
     * 是否人员档案
     */
    private Integer isMember;

    /**
     * 查询条件
     */
    public static LambdaQueryWrapper<SysUserEntity> getQueryWrapper(SysUserQueryForm params) {
        return new QueryWrapper<SysUserEntity>()
                .lambda()
                .eq(!ObjectUtils.isEmpty(params.getIsAdmin()), SysUserEntity::getIsAdmin, params.getIsAdmin())
                .and(!ObjectUtils.isEmpty(params.getKey()), wp -> wp
                        .like(SysUserEntity::getUsername, params.getKey())
                        .or()
                        .like(SysUserEntity::getRealName, params.getKey())
                        .or()
                        .like(SysUserEntity::getMobile, params.getKey())
                        .or()
                        .like(SysUserEntity::getIdcard, params.getKey())
                )
                .orderByDesc(SysUserEntity::getCreateTime);
    }
}
